﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace BenevolentSun
{
    public class IntroScreen : Screen
    {
        Video video;

        VideoPlayer videoPlayer = new VideoPlayer();

        double delay = 0;

        double maxDelay = 0.5;


        public override void LoadContent()
        {
            video = Content.Load<Video>("Video/Intro");
            
            base.LoadContent();
        }

        public override void UnloadContent()
        {
            if (videoPlayer.IsDisposed == false)
            {
                if (videoPlayer.State == MediaState.Playing)
                {
                    videoPlayer.Stop();
                }
                videoPlayer.Dispose();
            }
            base.UnloadContent();
        }

        public override void Update(GameTime gameTime, bool isFocusAvailable, bool isOccluded)
        {
            if (delay < maxDelay)
            {
                delay += gameTime.ElapsedGameTime.TotalSeconds;
                if (delay >= maxDelay)
                {
                    videoPlayer.Play(video);
                }
            }
            else
            {
                if (isFocusAvailable && IsExiting == false)
                {
                    if (videoPlayer.State == MediaState.Stopped)
                    {
                        NextScreen();
                    }
                }
            }
            base.Update(gameTime, isFocusAvailable, isOccluded);
        }


        public override void HandleInput()
        {
            if (ActionMap.Skip)
            {
                NextScreen();
            }

            base.HandleInput();
        }

        private void NextScreen()
        {
            ScreenManager.AddScreen(new LoadProfileScreen());
            ExitScreen();
        }


        public override void Draw(GameTime gameTime)
        {
            if (videoPlayer.State != MediaState.Stopped)
            {
                Texture2D texture = videoPlayer.GetTexture();

                SpriteBatch.Begin();//SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
                SpriteBatch.Draw(texture, new Vector2(0, 0), Color.White);
                SpriteBatch.End();
            }

            base.Draw(gameTime);
        }

    }
}
